<template>
  <div>
    <div v-if="blogType===0">
      <div class="marBox">
        <el-button type="primary" size="medium" @click="showBlogView(1)">新增博客</el-button>
      </div>
      <el-table :data="blogList" border style="width: 100%">
        <el-table-column prop="blogId" width="100" label="ID"></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column label="标签">
          <template slot-scope="scope">
            <el-tag :key="tag.tagId" v-for="tag in scope.row.tags">{{tag.name}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="200"></el-table-column>
        <el-table-column label="创建人" width="100">
          <template slot-scope="scope">
            <span>{{scope.row.user.userName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否发布" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isRelease"
              active-color="#13ce66"
              inactive-color="#ff4949"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="danger" @click="remove(scope.row.id)" size="small">移除</el-button>
            <el-button type="primary" size="small" @click="showBlogView(2,scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="marBox">
        
      </div>
    </div>
    <oper-blog @gotoIndex="gotoIndex" :blogObj="blogObj" v-else />
  </div>
</template>

<script>
import operBlog from "./operBlog";
export default {
  components: {
    operBlog
  },
  data() {
    return {
      blogType: 0, //0默认，展示blog列表，1新增，2修改
      blogObj: {},
      blogList: [],
    };
  },
  mounted() {
    this.getBlog();
  },
  methods: {
    showBlogView(num, item) {
      this.blogObj = {};
      this.blogType = num;
      if (num == 2) {
        //修改博客
        this.blogObj = item;
      }
    },
    getBlog() {
      this.$axios
        .get("/api/blog", {
          params: { page: this.page, pagesize: this.pagesize }
        })
        .then(res => {
          this.blogList = res.data;
          this.total=this.blogList.length;
        });
    },
    gotoIndex() {
      this.blogType = 0;
      this.getBlog();
    },
    sizeChang(val){
      this.pagesize=val;
      this.getBlog()
    }
  }
};
</script>
<style lang="scss" scoped>
.el-tag {
  margin-right: 10px;
}
.marBox {
  margin: 10px 0px;
}
</style>
